package batch;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.junit.Test;

import util.DBPoolUtils;

public class BatchDemo {
	@Test
	public void batch() throws SQLException {
		// 1.得到连接
		Connection c = DBPoolUtils.getConnection();
		// 2.得到句柄
		String sql = "insert into student values(?,?)";
		PreparedStatement ps = c.prepareStatement(sql);
		
		// 3.循环设置参数
		for(int i = 6; i<= 10000; i++) {
			// 4.设置参数
			ps.setInt(1, i);
			ps.setString(2, "a"+i);  // insert into student values(6,"A6")
			// 5.添加到批处理
			ps.addBatch();
			
			// 每100条执行一次批处理
			if (i % 100 == 0) {
				ps.executeBatch();
				// 清空批处理
				ps.clearBatch();
			}
		}
		// 关闭
		ps.close();
		c.close();
	}
}
